using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Infrastructure.Logger
{
    /// <summary>
    /// http请求监控 日志对象
    /// </summary>
    public class HttpRequestMonitorLog
    {
        public string ControllerName { get; set; }

        public string ActionName { get; set; }

        public DateTime ExecuteStartTime { get; set; }

        public DateTime ExecuteEndTime { get; set; }

        /// <summary>
        /// Form 表单数据
        /// </summary>
        public NameValueCollection FormCollections { get; set; }

        /// <summary>
        /// URL 参数
        /// </summary>
        public NameValueCollection QueryCollections { get; set; }

        public NameValueCollection ParamsCollections { get; set; }

        /// <summary>
        /// Form 表单数据
        /// </summary>
        public NameValueCollection RouteDataCollections { get; set; }

        /// <summary>
        /// 监控类型
        /// </summary>
        public enum LogMonitorType
        {
            Action = 1,
            View
        }

        /// <summary>
        /// 获取监控指标日志
        /// </summary>
        /// <param name="mtype"></param>
        /// <returns></returns>
        public string GetLoginfo(LogMonitorType mtype = LogMonitorType.Action)
        {
            if (mtype == LogMonitorType.Action)
            {
                string Msg = @"{0}{1}/{2},开始时间：{3},结束时间：{4},总时间：{5}毫秒";
                var routeDataStr = GetCollections(RouteDataCollections);
                var formStr = GetCollections(FormCollections);
                var queryStr = GetCollections(QueryCollections);

                var result = string.Format(Msg,
                    "Action执行时间监控：", ControllerName, ActionName,
                    ExecuteStartTime.ToString("MM-dd HH:mm:ss"),
                    ExecuteEndTime.ToString("MM-dd HH:mm:ss"),
                    (ExecuteEndTime - ExecuteStartTime).TotalMilliseconds);
                if (!string.IsNullOrWhiteSpace(routeDataStr))
                {
                    result += @"
        RouteDat：" + routeDataStr;
                }
                if (!string.IsNullOrWhiteSpace(formStr))
                {
                    result += @"
        Form表单数据：" + formStr;
                }
                if (!string.IsNullOrWhiteSpace(queryStr))
                {
                    result += @"
        URL参数：" + queryStr;
                }
                return result;
            }
            else if (mtype == LogMonitorType.View)
            {
                string Msg = @"{0}{1}/{2},开始时间：{3},结束时间：{4},总时间：{5}毫秒";
                return string.Format(Msg,
                    "View视图生成时间监控：", ControllerName, ActionName,
                    ExecuteStartTime.ToString("MM-dd HH:mm:ss"),
                    ExecuteEndTime.ToString("MM-dd HH:mm:ss"),
                    (ExecuteEndTime - ExecuteStartTime).TotalMilliseconds);
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取Post 或Get 参数
        /// </summary>
        /// <param name="Collections"></param>
        /// <returns></returns>
        public string GetCollections(NameValueCollection Collections)
        {
            string Parameters = string.Empty;
            if (Collections == null || Collections.Count == 0)
            {
                return Parameters;
            }
            foreach (string key in Collections.Keys)
            {
                Parameters += string.Format("{0}={1}&", key, Collections[key]);
            }
            if (!string.IsNullOrWhiteSpace(Parameters) && Parameters.EndsWith("&"))
            {
                Parameters = Parameters.Substring(0, Parameters.Length - 1);
            }
            return Parameters;
        }

    }
}
